package main.java.com.chenchangjie.Repository.impl;

import main.java.com.chenchangjie.Repository.UserRepository;
import main.java.com.chenchangjie.eneity.User;
import main.java.com.chenchangjie.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class userRepositoryImpl implements UserRepository {
    @Override
    public Object findUserByNamePassword(String name, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Object o = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select * from user where name=? and password = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setString(2,password);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                o = new User(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                JDBCUtil.Close(resultSet,statement,connection);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return o;
    }
}
